require('../css/index.less')
require('../lib/swiper/swiper-bundle.css')

document.ready(function () {

  //底部导航
  window.utils.addTab(0)

  // 判断用户是否登录，若没登陆，跳转到登录页
  window.utils.checkUserLogin()

  //轮播
  var mySwiper = new Swiper('.swiper-container', {
    direction: 'horizontal', // 垂直切换选项vertical
    loop: true, // 循环模式选项

    // 如果需要分页器
    pagination: {
      el: '.swiper-pagination',
    },
    //自动播放
    autoplay: {
      delay: 2000,
      stopOnLastSlide: false,
      disableOnInteraction: true,
    },
  })

  //获取 dom 节点
  let rankNum = document.querySelector('#rankNum'); //今日排名
  let punchDay = document.querySelector('#punchDay'); //累计打卡
  let cardDay = document.querySelector('#cardDay'); //今日打卡

  //获取本地数据
  let user = JSON.parse(localStorage.getItem('user'));

  // 处理打卡
  function handlePunch() {
    $http.get(`/clockIn?userId=${user.userId}`, function (res) {
      //打卡成功
      if (res.status === 0) {
        cardDay.textContent = '已打卡';
        cardDay.setAttribute('class', 'day-card yet-day-card');
        //累计打卡天数+1
        punchDay.textContent = Number(punchDay.textContent) + 1
        window.utils.toast("smile", '打卡成功')
        cardDay.removeEventListener('click', handlePunch); //解绑
      }
    })

  }

  //请求首页的信息
  $http.get(`/headPageInfo?userId=${user.userId}`, function (res) {
    if (res.status === 0) {
      // 修改DOM节点的textContent
      rankNum.textContent = res.data.rank
      punchDay.textContent = res.data.punchIn
      badgeNum.textContent = res.data.insigniaNum

      if (res.data.isPunch === "true") {
        cardDay.textContent = '已打卡'
        // cardDay.className = 'day-card yet-day-card';
        cardDay.setAttribute('class', 'day-card yet-day-card');

      } else {
        cardDay.textContent = '今日打卡';
        cardDay.className = 'day-card'
        cardDay.addEventListener('click', handlePunch); //解绑

      }
    }

  })



})